﻿using BCDto.Location.Driver;
using BCDto.Location.Horseman;
using BCService.TMS.Location.Driver;
using BCService.TMS.Location.Horseman;
using BCService.TMS.Order.Order;
using BCService.Utils;
using MongoDB.Bson;
using System;
using System.Collections.Generic;

namespace BCService.TMS.Location
{
    public class LocationService : ILocationService
    {
        private readonly IDriverLocationService driverLocationService;
        private readonly IHorsemanLocationService horsemanLocationService;
        private readonly IOrderProcessService orderProcessService;

        public LocationService(IDriverLocationService driverLocationService, IHorsemanLocationService horsemanLocationService, IOrderProcessService orderProcessService)
        {
            this.driverLocationService = driverLocationService;
            this.horsemanLocationService = horsemanLocationService;
            this.orderProcessService = orderProcessService;
        }

        #region 运输司机位置

        public DriverLocationBsonDto GetDriverLocationInfo(string vehicleNo)
        {
            return this.driverLocationService.GetDriverLocationInfo(vehicleNo);
        }



        public IEnumerable<DriverLocationNearDto> QueryNearDrivers(long orderId, int maxDistance)
        {
            var orderDto = this.orderProcessService.GetOrder(orderId);
            if (orderDto == null)
            {
                throw new ArgumentException("此订单不存在");
            }
            double[] location = new double[2] { orderDto.Longitude, orderDto.Latitude };
            List<BsonElement> query = new List<BsonElement>
            {
                new BsonElement("WorkingStatus",new BsonDocument
                {
                    { "$eq","Working"}
                })
            };
            List<BsonElement> projections = new List<BsonElement>
            {
                new BsonElement("VehicleTypeId", 1),
                new BsonElement("VehicleType", 1),
                new BsonElement("Longitude", 1),
                new BsonElement("Latitude", 1),
                new BsonElement("DriverId", 1),
                new BsonElement("Phone", 1),
                new BsonElement("Name", 1),
                new BsonElement("DriverPhotoPath", 1),
                new BsonElement("VehicleNo", 1),
                new BsonElement("SurplusWeight", 1),
                new BsonElement("CreditScore", 1),
                new BsonElement("Coordinate", 1),
                new BsonElement("VehicleWeight", 1),
                new BsonElement("WorkingStatus", 1),
                new BsonElement("VehicleLength", 1),
                new BsonElement("WorkingStatusStr", 1),
                new BsonElement("VehicleWidth", 1),
                new BsonElement("VehicleHeight", 1),
                new BsonElement("VehicleStere", 1),
                new BsonElement("IsAcceptAssign", 1),
                new BsonElement("IsReceivePartLoad", 1),
                new BsonElement("DailyReceivedOrders", 1),
                new BsonElement("TotalReceivedOrders", 1),
                new BsonElement("FullLoadRatio", 1),
                new BsonElement("dis", 1),
            };
            var locationDriverNearDtos = this.driverLocationService.GetGeoNearLocationsByCoordinate(location, maxDistance, query, projections);
            return locationDriverNearDtos.As<IEnumerable<DriverLocationNearDto>>();
        }
        #endregion


        #region 骑手位置

        public IEnumerable<HorsemanLocationNearDto> QueryNearHorsemen(long orderId, int maxDistance)
        {
            var orderDto = this.orderProcessService.GetOrder(orderId);
            if (orderDto == null)
            {
                throw new ArgumentException("此订单不存在");
            }
            double[] location = new double[2] { orderDto.Longitude, orderDto.Latitude };
            List<BsonElement> query = new List<BsonElement>
            {
                new BsonElement("WorkingStatus",new BsonDocument
                {
                    { "$eq","Working"}
                })
            };
            List<BsonElement> projections = new List<BsonElement>
            {
                new BsonElement("Longitude", 1),
                new BsonElement("Latitude", 1),
                new BsonElement("DriverId", 1),
                new BsonElement("Phone", 1),
                new BsonElement("Name", 1),
                new BsonElement("DriverPhotoPath", 1),
                new BsonElement("VehicleNo", 1),
                new BsonElement("CreditScore", 1),
                new BsonElement("Coordinate", 1),
                new BsonElement("WorkingStatus", 1),
                new BsonElement("WorkingStatusStr", 1),
                new BsonElement("DeliveryBoxLength", 1),
                new BsonElement("DeliveryBoxWidth", 1),
                new BsonElement("DeliveryBoxHeight", 1),
                new BsonElement("IsAcceptAssign", 1),
                new BsonElement("DailyReceivedOrders", 1),
                new BsonElement("TotalReceivedOrders", 1),
                new BsonElement("dis", 1),
            };
            var locationHorsemenNearDtos = this.horsemanLocationService.GetGeoNearLocationsByCoordinate(location, maxDistance, query, projections);
            return locationHorsemenNearDtos.As<IEnumerable<HorsemanLocationNearDto>>();
        }

        public HorsemanLocationBsonDto GetHorsemanLocationInfo(string vehicleNo)
        {
            return this.horsemanLocationService.GetHorsemanLocationInfo(vehicleNo);
        }
        #endregion


    }
}
